<?php
class BlocksController extends AppController {
    var $name = 'Blocks';
    
    var $uses = array('Blockarea','Block');
    
    function admin_index() {
        
        $options = array();
    
        
        $blockarea = $this->paginate('Blockarea', $options);
        $this->set(compact('blockarea'));
        

    }
    
    function admin_manage() {
        
        
        if(!empty($this->data)){
            Configure::write('debug',0);
            
            
            $this->data['Ajax']=explode(':',$this->data['Ajax']);  
            $tmp_block=explode('|',$this->data['Ajax'][0]);
            $inactives=explode(',',$this->data['Ajax'][1]);
            unset($inactives[count($inactives)-1]);
            
            $blocks=array();
            
            foreach($tmp_block as $item){
                if(!empty($item)){
                    $tmp=explode('-',$item);
                    $blocks[$tmp[0]]=explode(',',$tmp[1]);
                    unset($blocks[$tmp[0]][count($blocks[$tmp[0]])-1]);
                    
                }
            }
            
            
            $data=array();
            
            
            foreach($blocks as $area=>$block){
                foreach($block as $item){
                    $tmp=array();
                    $order=explode('.',$item);
                    $tmp['id']=$order[0];
                    $tmp['blockarea_name']=$area;
                    $tmp['status']='active';
                    $tmp['order']=$order[1];
                    $data[]=$tmp;
                }
            }
            
            
            $success=true;
            unset($this->Block->validate);
            
            
            if(count($data))
                if(!$this->Block->saveAll($data))$success=false;
            
            
            $data=array();
            
            
            foreach($inactives as $item){
                $tmp=array();
                $order=explode('.',$item);
                $tmp['id']=$order[0];
                $tmp['blockarea_name']='';
                $tmp['status']='inactive';
                $tmp['order']=$order[1];
                
                $data[]=$tmp;
            }
            
            if(count($data) && $success)
                if(!$this->Block->saveAll($data))$success=false;
                
            
            
            if($success){
                echo 'success';
                $this->Session->write('Success',true);
                $this->Session->setFlash('Blocks Saved Successfully!');
            }
                
            exit;
            
        }
        
        $options = array();
        $blocks = $this->Blockarea->find('all');
        $this->set(compact('blocks'));
        $inactives = $this->Block->find('all',array('conditions'=>array('status'=>'inactive')));
        $this->set(compact('inactives'));
        

    }
    
    function admin_add() {
        
        if(!empty($this->data)) :
            $this->data['Blockarea']['created']=date('Y-m-d H:i:s');
            $this->data['Blockarea']['modified']=date('Y-m-d H:i:s');
            
            if($this->Blockarea->save($this->data)) :
                $this->Session->write('Success',true);
                $this->Session->setFlash('Block Area Created Sucessfully');
                $this->redirect(array('action'=>'index'));
                exit;
            endif;
        endif;
        

    }
    
    function admin_edit($name=null) {
        if($name == null) :
            $this->redirect($this->referer());
            exit;
        endif;
        
        $this->set('id',$name);
        
        
        if(!empty($this->data)) :
            $this->data['Blockarea']['name']=$name;
            $this->data['Blockarea']['modified']=date('Y-m-d H:i:s');
            
            if($this->Blockarea->save($this->data)) :
                $this->Session->write('Success',true);
                $this->Session->setFlash('Block Area Created Sucessfully');
                $this->redirect(array('action'=>'index'));
                exit;
            endif;
        else:
            $this->data=$this->Blockarea->find(array('name'=>$name));
            $this->render('admin_add');
        endif;

    }
    
    function admin_delete($name = null) {
        if($name == null) :
            $this->redirect($this->referer());
            exit;
        endif;
        
        if($this->Blockarea->delete(array('name'=>$name))) :
            $this->Session->write('Success',true);
            $this->Session->setFlash('Block Area Deleted sucessfully');
        endif;
        
        $this->redirect($this->referer());
        exit;
    }
    
    
    function admin_add_block() {
        
        if(!empty($this->data)) :
            pr($this->data);
            
            $this->data['Block']['created']=date('Y-m-d H:i:s');
            $this->data['Block']['modified']=date('Y-m-d H:i:s');
            
            if($this->Block->save($this->data)) :
                $this->Session->write('Success',true);
                $this->Session->setFlash('Block Created Sucessfully');
                $this->redirect(array('action'=>'manage'));
                exit;
            endif;
        endif;
        

    }
    
    function admin_edit_block($id=null) {
        if($id == null) :
            $this->redirect($this->referer());
            exit;
        endif;
        
        $this->set('id',$id);
        
        
        if(!empty($this->data)) :
            $this->data['Block']['id']=$id;
            
            $this->data['Block']['modified']=date('Y-m-d H:i:s');
            
            
            
            if($this->Block->save($this->data)) :
                $this->Session->write('Success',true);
                $this->Session->setFlash('Block Updated Sucessfully');
                $this->redirect(array('action'=>'manage'));
                exit;
            else:
                $this->Session->setFlash('Block Update Failed');
                $this->redirect(array('action'=>'manage'));
                exit;
            endif;
        else:
            $this->data=$this->Block->find(array('id'=>$id));
            $this->render('admin_add_block');
        endif;

    }
    
    function admin_delete_block($id = null) {
        if($id == null) :
            $this->redirect($this->referer());
            exit;
        endif;
        
        if($this->Block->delete(array('id'=>$id))) :
            $this->Session->write('Success',true);
            $this->Session->setFlash('Block Deleted sucessfully');
        endif;
        
        $this->redirect($this->referer());
        exit;
    }
    
    
    
    
    
   
    
}
?>